package com.bxd.baodan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.bxd.baodan.R;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.util.AppManager;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.StringUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class FastQuestionActivity extends BaseActivity implements View.OnClickListener {

    EditText et_question;
    private String cateId;
    private TextView text_tag;
    private LinearLayout tag_ly;

    //   {"choice":3,"stepsnumber":2,"supplement":"发刚回家就看流量了开封府姑几节课","type":0}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fast_question);
        setActionBarTitle("快速提问");

        findViewById(R.id.btn_submit).setOnClickListener(this);
        text_tag = (TextView) findViewById(R.id.text_tag);
        tag_ly = (LinearLayout) findViewById(R.id.tag_ly);
        tag_ly.setOnClickListener(this);

        et_question = (EditText) findViewById(R.id.et_question);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //提交
            case R.id.btn_submit:
                if (loginStatus()) {
                    String edstr = et_question.getText().toString().trim();

                    if (edstr.isEmpty()) {
                        showToast("问题不能为空");
                        return;
                    }
                    if (edstr.length() < 5) {
                        showToast("字数这么少，真的把想问的说清楚了吗？");
                        return;
                    }
                    if (StringUtil.isEmpty(cateId)) {
                        Toast.makeText(FastQuestionActivity.this, "请选择问题分类", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    submitAsk(edstr);
                }
                break;
            case R.id.tag_ly:
                openActivityForResult(QuestionTagActivity.class, new String[]{}, new String[]{});
                break;

        }
    }


    /**
     * 开始提问
     */
    protected void submitAsk(String str) {
        // {"choice":3,"stepsnumber":2,"supplement":"发刚回家就看流量了开封府姑几节课","type":0}
        AskItem askItem = new AskItem(4, 2, str, 0);
        String jString = new Gson().toJson(askItem);

        NetUtil netUtil = new NetUtil(this,
                JsonApi.ASK_QUESTION);
        netUtil.setParams("options", jString);
        netUtil.setParams("content", et_question.getText().toString().trim());
        netUtil.setParams("cateId",cateId );
        netUtil.postRequest("正在发起提问", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {

                if (JSONUtil.isSuccess(result)) {

                    try {
                        JSONObject json = new JSONObject(JSONUtil
                                .getData(result));
                        long qid = Long.parseLong(json.getString("qid"));
                        AppManager.getInstance().finishOtherActivity(MainActivity.class);
                      //  openActivity(AllQuestionActivity.class);
                        openActivity(QuestionDetailActivity.class, new
                                String[]{"qid"}, new String[]{qid +""});
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    public class AskItem {

        // {"choice":3,"stepsnumber":2,"supplement":"发刚回家就看流量了开封府姑几节课","type":0}
        public int choice;
        public int stepsnumber;
        public String supplement;
        public int type;

        public AskItem(int choice, int stepsnumber, String supplement, int type) {
            this.choice = choice;
            this.stepsnumber = stepsnumber;
            this.supplement = supplement;
            this.type = type;
        }
    }


    // 回调的方式来获取指定Activity返回的结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 0) {
            try {
                cateId = data.getStringExtra("tagId");
                String name = data.getStringExtra("tagName");
                text_tag.setText(name);
            } catch (Exception e) {

            }
        }
    }
}
